package com.lemon.smartsafety.ui.adapter.safe.data;

import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.CardView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.android.http.HttpUtils;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.DataInfo;
import com.lemon.smartsafety.entity.safe.company.CompanyBuildingVo;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafety.utils.common.AnimationHelper;
import com.lemon.smartsafety.utils.common.LoggerHelper;
import com.lemon.smartsafety.utils.common.StringUtils;
import com.lemon.smartsafety.utils.photo.GlideImageLoader;

import java.util.List;

import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

/**
 * Created by Cooliean on 16/2/22.
 * 企业信息列表适配
 */
public class BuildingListAdapter extends LemonRcvAdapter<CompanyBuildingVo> {
    private LoggerHelper loggerHelper = new LoggerHelper(BuildingListAdapter.class);
    private RecyclerTClickListener<CompanyBuildingVo> mListener;
    /**
     * 是否是安全巡查界面过来的
     */
    private boolean isSafeInspection = false;

    private BuildingListAdapterItem mItem = null;

    public BuildingListAdapter(@Nullable List<CompanyBuildingVo> data, RecyclerTClickListener<CompanyBuildingVo> listener) {
        super(data);
        mListener = listener;
    }

    public BuildingListAdapter(@Nullable List<CompanyBuildingVo> data, RecyclerTClickListener<CompanyBuildingVo> listener, boolean safeInspection) {
        super(data);
        mListener = listener;
        this.isSafeInspection = safeInspection;
    }

    private BuildingListAdapter(@Nullable List<CompanyBuildingVo> data) {
        super(data);
    }


    @Override
    public Object getItemType(CompanyBuildingVo data) {
        return -1;
    }


    @NonNull
    @Override
    public AdapterItem createItem(Object type) {
        return getListItem();
    }


    private BuildingListAdapterItem getListItem() {
        if (mItem != null) return mItem;
        return new BuildingListAdapterItem(mListener);
    }


    /**
     * Created by Cooliean on 16/1/27.
     * 建筑列表项目
     */
    public class BuildingListAdapterItem implements AdapterItem<CompanyBuildingVo> {
        private LinearLayout ripple;
        private CardView   mainCardView;
        private ImageView  ivIcon;
        private ImageView  ivStatusLight;
        private TextView   tvBuildTitle;
        private TextView   tvBuildType;
        private TextView   tvBuildRemark;


        private RecyclerTClickListener<CompanyBuildingVo> mListener;
        private CompanyBuildingVo                         mData;
        private int                                       mPosition;

        public BuildingListAdapterItem(RecyclerTClickListener<CompanyBuildingVo> recycleItemClickListener) {
            this.mListener = recycleItemClickListener;
        }

        @Override
        public int getLayoutResId() {
            return R.layout.item_building_list;
        }

        @Override
        public void bindViews(View root) {
            ripple = (LinearLayout) root.findViewById(R.id.ripple);
            mainCardView = (CardView) root.findViewById(R.id.main_card_view);
            ivIcon = (ImageView) root.findViewById(R.id.iv_icon);
            ivStatusLight = (ImageView) root.findViewById(R.id.iv_status_light);
            if (isSafeInspection) {
                ivStatusLight.setVisibility(View.VISIBLE);
            } else {
                ivStatusLight.setVisibility(View.GONE);
            }
            tvBuildTitle = (TextView) root.findViewById(R.id.tv_build_title);
            tvBuildType = (TextView) root.findViewById(R.id.tv_build_type);
            tvBuildRemark = (TextView) root.findViewById(R.id.tv_build_remark);
        }

        @Override
        public void setViews() {
            ripple.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onItemClick(mData, mPosition);
                }
            });


       /*     ripple.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
                @Override
                public void onComplete(RippleView rippleView) {
                    mListener.onItemClick(mData, mPosition);
                }
            });*/
        }

        @Override
        public void handleData(CompanyBuildingVo data, int position) {
            mData = data;
            mPosition = position;
            GlideImageLoader.loadImage(ivIcon.getContext(), HttpUtils.formatFilesUrl(data.getImageUrl()), ivIcon);
            tvBuildTitle.setText(StringUtils.nullValue(data.getBuildingTitle()));
            tvBuildRemark.setText(StringUtils.nullValue(data.getRemark()));
            tvBuildType.setText(StringUtils.nullValue(data.getBuildType().getName()));
            if (isSafeInspection) {
                String statusString = StringUtils.nullValue(data.getStatus(), "0");
                Log.v("THE STATUS is :",statusString);
                setStatusLight(statusString, ivStatusLight);
            }
        }


    }

    public void setStatusLight(int position, ImageView ivStatusLight) {
        try {
            String statusString = StringUtils.nullValue(mDataList.get(position).getStatus(), "0");
            setStatusLight(statusString, ivStatusLight);
        } catch (Exception e) {

        }
    }

    public static void setStatusLight(String status, ImageView ivStatusLight) {
        if (ivStatusLight == null) return;
        String   statusString = status;
        Drawable drawable     = ivStatusLight.getDrawable();
        AnimationHelper.stopAlphaAnimation(ivStatusLight);
        if (DataInfo.RED_TAG.equals(statusString)) {

            ivStatusLight.setBackgroundResource(R.drawable.shape_circle_red);

            //ivStatusLight.setImageDrawable(DrawableUtils.$tint(drawable, ivStatusLight.getContext().getResources().getColor(R.color.colorAppRed)));
            //AnimationHelper.startAlphaAnimation(ivStatusLight, AnimationHelper.STATUS_IMAGE_RED_DELAY);
        } else if (DataInfo.YELLOW_TAG.equals(statusString)) {

            ivStatusLight.setBackgroundResource(R.drawable.shape_circle_yellow);
            //ivStatusLight.setImageDrawable(DrawableUtils.$tint(drawable, ivStatusLight.getContext().getResources().getColor(R.color.colorAppYellow)));
            //AnimationHelper.startAlphaAnimation(ivStatusLight, AnimationHelper.STATUS_IMAGE_YELLOW_DELAY);
        } else {

            ivStatusLight.setBackgroundResource(R.drawable.shape_circle_green);
            //ivStatusLight.setImageDrawable(drawable.$tint(drawable, ivStatusLight.getContext().getResources().getColor(R.color.colorAppGreen)));
            //AnimationHelper.stopAlphaAnimation(ivStatusLight);
        }
    }
}
